-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
created API, Rick and Morty #181
base: main
Are you sure you want to change the base?
Conversation
@Override | ||
public CharacterDto getRandomCharacter() { | ||
List<Character> characters = characterRepository.findAll(); | ||
if (characters.isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CharacterServiceImpl implements CharacterService { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't find service where you use
You must use public API (you should use REST API).
HttpRequest httpRequest = HttpRequest.newBuilder() | ||
.GET() | ||
.uri(URI.create(URL)) | ||
.build(); | ||
HttpResponse<String> response = httpClient | ||
.send(httpRequest, HttpResponse.BodyHandlers.ofString()); | ||
|
||
JSONObject jsonResponse = new JSONObject(response.body()); | ||
JSONArray resultsArray = jsonResponse.getJSONArray("results"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this only gets 20 characters (one page), not all of them, use .next() to get next page
@Override | ||
public CharacterDto getRandomCharacter() { | ||
List<Character> characters = characterRepository.findAll(); | ||
Random random = new Random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better extract random to a class level field, not to recreate it needlessly per each method invocation
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CharacterMapper { | ||
CharacterDto toDo(Character character); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CharacterDto toDo(Character character); | |
CharacterDto toDto(Character character); |
nextPageUrl = | ||
jsonResponse.isNull("next") ? | ||
null : jsonResponse.getJSONObject("info").getString("next"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't look nice tbh ,let's use objectMapper.readValue() to read JSON into DTOs
public class CharacterServiceImpl implements CharacterService { | ||
private final CharacterRepository characterRepository; | ||
private final CharacterMapper characterMapper; | ||
private static final Random random = new Random(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private static final Random random = new Random(); | |
private final Random random = new Random(); |
No description provided.